package com.biShe.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.biShe.entity.*;
import com.biShe.mapper.*;
import com.biShe.utils.Result;
import com.biShe.utils.UploadImg;
import com.biShe.vo.CategoryVO;
import com.biShe.vo.GoodsListVO;
import com.biShe.vo.GoodsVO;
import com.biShe.vo.PropertyVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小刘
 * @since 2023-05-06
 */
@Configuration
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsCPropertTypeMapper goodsCPropertTypeMapper;

    @Autowired
    PropertyTypeMapper propertyTypeMapper;

    @Autowired
    PropertDetailMapper propertDetailMapper;

    @GetMapping("")
    public GoodsVO getAloods(){
        GoodsVO goodsVO = new GoodsVO();
        List<CategoryVO> categoryVOS = new ArrayList<>();
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<Category>();
        List<Category> categories = categoryMapper.selectList(categoryQueryWrapper);
        for (Category cg:categories) {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(cg,categoryVO);
            List<GoodsListVO> goodsListVOS = new ArrayList<>();

            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
            goodsQueryWrapper.eq("category",cg.getId());
            List<Goods> goods = goodsMapper.selectList(goodsQueryWrapper);
            for (Goods gs:goods) {
                GoodsListVO goodsListVO = new GoodsListVO();
                BeanUtils.copyProperties(gs,goodsListVO);
                List<PropertyVO> propertyVOS = new ArrayList<>();
                QueryWrapper<GoodsCPropertType> goodsCPropertTypeQueryWrapper = new QueryWrapper<>();
                goodsCPropertTypeQueryWrapper.eq("goods_id",gs.getId());
                List<GoodsCPropertType> goodsCPropertTypes = goodsCPropertTypeMapper.selectList(goodsCPropertTypeQueryWrapper);
                for (GoodsCPropertType gt:goodsCPropertTypes) {
                    PropertyType pt = propertyTypeMapper.selectById(gt.getPropertyType());
                    PropertyVO propertyVO = new PropertyVO();
                    BeanUtils.copyProperties(pt,propertyVO);
                    QueryWrapper<PropertDetail> propertDetailQueryWrapper = new QueryWrapper<>();
                    propertDetailQueryWrapper.eq("property_type_id",pt.getId());
                    List<PropertDetail> propertDetailList = propertDetailMapper.selectList(propertDetailQueryWrapper);
                    propertyVO.setValues(propertDetailList);
                    propertyVOS.add(propertyVO);
                }

                goodsListVO.setProperty(propertyVOS);
                goodsListVOS.add(goodsListVO);
            }
            categoryVO.setGoods_list(goodsListVOS);
            categoryVOS.add(categoryVO);
        }

        goodsVO.setGoods(categoryVOS);
        return goodsVO;
    }

    /**
     * 分页查找所有商品
     *
     * @return 返回所有用户信息列表
     */
    @ApiOperation(value="获取所有商品信息", notes="返回所有商品信息列表")
    @GetMapping("/list/{page}/{size}")
    public Result list(@PathVariable("page") String page, @PathVariable("size") String size) {
        Page pageHelper = PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(size));
        List<CategoryVO> userList = getAloods().getGoods();
        PageInfo pageInfo = new PageInfo(userList);
        return Result.succ(pageInfo);
    }


    @PostMapping("/upImg")
    public String upImg(MultipartFile file) throws Exception{
       return UploadImg.upLoadImage(file);
    }

}

